/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.DTO.OutputEnvironmentFactorDTO;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnterpriseType;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentFactor;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentIndex;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentScore;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseTypeService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentService;
import com.opensymphony.xwork2.ActionSupport;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-20
 * 类描述：银行环境模块的action封装类
 */
public class BankEnvironmentAction extends ActionSupport implements ServletContextAware {
    private int enterpriseId;//企业的id\
    private int factorId;//环境指标的id
    private List<EnterpriseType> typeList;//所有企业的类型
    private List<Enterprise> enterpriseList;//所有企业的list
    private List<EnvironmentFactor> scoreList;//所有环境评分指标
    private List<EnvironmentFactor> indexList;//所有环境核心指标
    private List<OutputEnvironmentFactorDTO> factorDTOList;//环境指标前台显示DTOlist
    private OutputEnvironmentFactorDTO factorDTO;//某个环境指标的DTO
    private List<EnvironmentScore> scoreHistory;//某个具体财务指标的历史记录
    private List<EnvironmentIndex> indexHistory;//某个具体财务指标的历史记录
    private BufferedImage image;//某个财务指标的折线图
    private File file;//临时图片文件
    private ServletContext context;
    
    
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }
    
    /**
     * 方法描述：企业跳转至环境主界面
     * param：
     * return：SUCCESS
     */
    public String lookup_Environment(){
        //取得所有企业类型
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.typeList = ets.getTypeList();
        //取得所有企业
        EnterpriseService us = ServiceFactory.getEnterpriseServiceInstance();
        this.enterpriseList = us.getAllEnterprise();
        //得到所有环境因素
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.scoreList = es.get_AllScoreFactor();
        this.indexList = es.get_AllIndexFactor();
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：查看所有环境的评分指标概况
     * param：
     * return：SUCCESS
     */
    public String lookup_All(){
        //得到所有环境因素
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.factorDTOList = es.lookup_AllEnvironmentFactor(enterpriseId);
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：企业查看某个环境的评分指标
     * param：
     * return：SUCCESS
     */
    public String lookup_ScoreFactor(){
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.factorDTO = es.lookup_A_ScoreFactor(enterpriseId, factorId);//取得该指标的概况
        this.scoreHistory = es.get_ScoreFactorHistory(enterpriseId, factorId);
        this.image = es.get_FactorImage(enterpriseId, factorId); 
        
        //生成一个临时图片以供图片显示(先删除上次生成的临时图片)
        if(file != null){
            file.delete();
        }
        file = new File(context.getRealPath("/bank/image"),"temp"+enterpriseId+System.currentTimeMillis()+".png");
        try {
            ImageIO.write(image, "png", file);
            image.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：企业查看某个环境的核心指标
     * param：
     * return：SUCCESS
     */
    public String lookup_IndexFactor(){
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.factorDTO = es.lookup_A_IndexFactor(enterpriseId, factorId);//取得该指标的概况
        this.indexHistory = es.get_IndexFactorHistory(enterpriseId, factorId);
        
        this.image = es.get_FactorImage(enterpriseId, factorId); 
        
        //生成一个临时图片以供图片显示(先删除上次生成的临时图片)
        if(file != null){
            file.delete();
        }
        file = new File(context.getRealPath("/bank/image"),"temp"+enterpriseId+System.currentTimeMillis()+".png");
        try {
            ImageIO.write(image, "png", file);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：刷新环境下该企业才有的指标
     * param：id 企业的id
     * return：
     */
    public void refreshIndexList(int id){
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.indexList = es.get_IndexFactor(id);
    }
    
    
    
    
    

    public List<EnvironmentFactor> getIndexList() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        for(int i=0; i<indexList.size(); i++){
            indexList.get(i).setTotal(Double.parseDouble(df.format(indexList.get(i).getTotal()).replace(",", "")));
        }
        return indexList;
    }

    public void setIndexList(List<EnvironmentFactor> indexList) {
        this.indexList = indexList;
    }

    public List<EnvironmentFactor> getScoreList() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        for(int i=0; i<scoreList.size(); i++){
            scoreList.get(i).setTotal(Double.parseDouble(df.format(scoreList.get(i).getTotal()).replace(",", "")));
        }
        return scoreList;
    }

    public void setScoreList(List<EnvironmentFactor> scoreList) {
        this.scoreList = scoreList;
    }

    public List<OutputEnvironmentFactorDTO> getFactorDTOList() {
        for(int i=0; i<factorDTOList.size(); i++){
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
            factorDTOList.get(i).setLatestValue(Double.parseDouble(df.format(factorDTOList.get(i).getLatestValue()).replace(",", "")));
            factorDTOList.get(i).setMaxValue(Double.parseDouble(df.format(factorDTOList.get(i).getMaxValue()).replace(",", "")));
            factorDTOList.get(i).setMinValue(Double.parseDouble(df.format(factorDTOList.get(i).getMinValue()).replace(",", "")));
            factorDTOList.get(i).setUpdownMargin(Double.parseDouble(df.format(factorDTOList.get(i).getUpdownMargin()).replace(",", "")));
            factorDTOList.get(i).setUpdownValue(Double.parseDouble(df.format(factorDTOList.get(i).getUpdownValue()).replace(",", "")));
        }
        return factorDTOList;
    }

    public void setFactorDTOList(List<OutputEnvironmentFactorDTO> factorDTOList) {
        this.factorDTOList = factorDTOList;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public OutputEnvironmentFactorDTO getFactorDTO() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        factorDTO.setLatestValue(Double.parseDouble(df.format(factorDTO.getLatestValue()).replace(",", "")));
        factorDTO.setMaxValue(Double.parseDouble(df.format(factorDTO.getMaxValue()).replace(",", "")));
        factorDTO.setMinValue(Double.parseDouble(df.format(factorDTO.getMinValue()).replace(",", "")));
        factorDTO.setUpdownMargin(Double.parseDouble(df.format(factorDTO.getUpdownMargin()).replace(",", "")));
        factorDTO.setUpdownValue(Double.parseDouble(df.format(factorDTO.getUpdownValue()).replace(",", "")));
        return factorDTO;
    }

    public void setFactorDTO(OutputEnvironmentFactorDTO factorDTO) {
        this.factorDTO = factorDTO;
    }

    public int getFactorId() {
        return factorId;
    }

    public void setFactorId(int factorId) {
        this.factorId = factorId;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public List<EnvironmentIndex> getIndexHistory() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        for(int i=0; i<indexHistory.size(); i++){
            indexHistory.get(i).setValue(Double.parseDouble(df.format(indexHistory.get(i).getValue()).replace(",", "")));
        }
        return indexHistory;
    }

    public void setIndexHistory(List<EnvironmentIndex> indexHistory) {
        this.indexHistory = indexHistory;
    }

    public List<EnvironmentScore> getScoreHistory() {DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        for(int i=0; i<scoreHistory.size(); i++){
            scoreHistory.get(i).setValue(Double.parseDouble(df.format(scoreHistory.get(i).getValue()).replace(",", "")));
        }
        return scoreHistory;
    }

    public void setScoreHistory(List<EnvironmentScore> scoreHistory) {
        this.scoreHistory = scoreHistory;
    }

    public List<Enterprise> getEnterpriseList() {
        return enterpriseList;
    }

    public void setEnterpriseList(List<Enterprise> enterpriseList) {
        this.enterpriseList = enterpriseList;
    }

    public List<EnterpriseType> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<EnterpriseType> typeList) {
        this.typeList = typeList;
    }

    @Override
    public void setServletContext(ServletContext sc) {
        this.context = sc;
    }
}
